// LEC    March 26, 2021
//
// STATE SIZE PAPER
// Analysis file system level with Centennia Data



cd $OUTDIR

use ethbc, clear

tab birtha deathb

tab birtha deathb if ethchange==1


cd $OUTDIR

use analysis_data, clear

drop if year<$STARTYEAR

/*
gen id1886 = .
replace id1886 = 1 if oldstate==0

bys year: egen n1 = count(id) if id1886 != .
bys year: egen n2 = count(id) if id1886 == .

gen area1 = .
replace area1 = area if id1886 != . 

gen area2 = .
replace area2 = area if id1886 == . & n2>10

gen maxshare1 = .
replace maxshare1 = maxshare if id1886 != .

gen maxshare2 = .
replace maxshare2 = maxshare if id1886 == .

gen ef1 = .
replace ef1 = ef if id1886 != .

gen ef2 = .
replace ef2 = ef if id1886 == . & n2>10

gen agfrac1 = .
replace agfrac1 = agfrac if id1886 != .

gen agfrac2 = .
replace agfrac2 = agfrac if id1886 == . & n2>10
*/

bys year: egen sumarea = sum(area)

bys year: egen sysconc = sum((area/sumarea)^2)


// Weighted means

bys year: egen efaw = sum(area*ef)
replace efaw = efaw/sumarea

bys year: egen agfracaw = sum(area*agfrac)
replace agfracaw = agfracaw/sumarea


bys year: egen maxshareaw = sum(area*maxshare)
replace maxshareaw = maxshareaw/sumarea

bys year: egen maxagshareaw = sum(area*maxagshare)
replace maxagshareaw = maxagshareaw/sumarea


bys year: egen intdjaw = sum(area*intdj)
replace intdjaw = intdjaw/sumarea

bys year: egen extdjaw = sum(area*extdj)
replace extdjaw = extdjaw/sumarea




bys year: egen sumarea1 = sum(area*oldstate)
bys year: egen sumarea2 = sum(area*(1-oldstate))

bys year: egen efaw1 = sum(area*ef*oldstate)
replace efaw1 = efaw1/sumarea1

bys year: egen agfracaw1 = sum(area*agfrac*oldstate)
replace agfracaw1 = agfracaw1/sumarea1

bys year: egen efaw2 = sum(area*ef*(1-oldstate))
replace efaw2 = efaw2/sumarea2

bys year: egen agfracaw2 = sum(area*agfrac*(1-oldstate))
replace agfracaw2 = agfracaw2/sumarea2



bys year: egen sumpop0=sum(population)

bys year: egen efpw = sum(population*ef)
replace efpw = efpw/sumpop0

bys year: egen agfracpw = sum(population*agfrac)
replace agfracpw = agfracpw/sumpop0

bys year: egen sumpop1 = sum(population*oldstate)
bys year: egen sumpop2 = sum(population*(1-oldstate))

bys year: egen efpw1 = sum(population*ef*oldstate)
replace efpw1 = efpw1/sumpop1

bys year: egen agfracpw1 = sum(population*agfrac*oldstate)
replace agfracpw1 = agfracpw1/sumpop1

bys year: egen efpw2 = sum(population*ef*(1-oldstate))
replace efpw2 = efpw2/sumpop2

bys year: egen agfracpw2 = sum(population*agfrac*(1-oldstate))
replace agfracpw2 = agfracpw2/sumpop2

bys year: egen syspopconc = sum((population/sumpop0)^2)


sort idc year
xtset idc year


bys year: egen medarea = median(area)


//////////// Collapse data to system level ///////////////////////////////////////////////////


collapse (max)n /*n1 n2*/ (mean)sumarea area medarea /*area1 area2*/ lnarea maxshare /*maxshare1 maxshare2*/ ///
(mean)ef /*ef1 ef2*/ agfrac /*agfrac1 agfrac2*/ efaw /*efaw1 efaw2*/ agfracaw /*agfracaw1 agfracaw2*/ efpw agfracpw /*efpw1 agfracpw1 efpw2 agfracpw2*/ maxagshare maxshareaw maxagshareaw ///
intdj intdjaw extdj extdjaw ///
(mean)sysconc syspopconc /*(mean)wrsysconc*/ (sum)terrnull transfera absorba absorbb secessiona secessionb collapsea collapseb ethseca ethsecb irra ethabsorbb ///
(sum) ethcollapsea ethcollapseb, by (year) ///
// cumularea lncumularea cumuletharea lncumuletharea cumulnetharea lncumulnetharea


drop if year<$STARTYEAR


tset year

gen bc = transfera + absorbb + secessiona + collapsea
gen ethbc = irra + ethabsorbb + ethseca + ethcollapseb

gen neabsorb = absorbb - ethabsorbb
gen nesec = secessiona - ethseca
gen necollapsea = collapsea - ethcollapsea
gen necollapseb = collapseb - ethcollapseb

// net border change
gen dn = secessiona + collapsea - absorbb -collapseb + terrnull
gen ethdn = ethseca + ethcollapsea - ethabsorbb - ethcollapseb +terrnull
gen nethdn = nesec + necollapsea - neabsorb - necollapseb + terrnull

gen one = 1
bys one (year): gen cumuldn = sum(dn)
bys one (year): gen cumulethdn = sum(ethdn)
bys one (year): gen cumulnethdn = sum(nethdn)

gen startyear = 0
replace startyear = 1 if year == $STARTYEAR

bys one: egen n0 = max(startyear*n)
bys one: egen area0 = max(startyear*sumarea)

gen reln = n0 + cumuldn
gen delta = reln - n

gen totarea = sumarea/(n0 + cumuldn)
gen etharea = sumarea/(n0 + cumulethdn)
gen netharea = sumarea/(n0 + cumulnethdn)



gen neth = n0 + cumulethdn
gen nneth = n0 + cumulnethdn





////////////////////////////////////////////////////////////////////////
 
global G xla(1880(20)2003)  graphregion(color(white)) bgcolor(white)
global G xla(1880(10)2003)  graphregion(color(white)) bgcolor(white)
global G2 xla(1850(50)2003)  graphregion(color(white)) bgcolor(white)
global R "(1800(50)2020)"
global R1 "(1880(10)2020)"   

 cd $OUTPUTDIR
 
/*
tsline area, yla(0(500000)2500000) xla(1800(50)2003) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export StateSize_Europe.png, width(1275) replace
graph export StateSize_Europe.pdf, replace

tsline n, xla(1800(50)2020) yla(0(10)60) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export NStates_Europe.png, width(1275) replace
graph export NStates_Europe.pdf, replace


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2003) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
graph export FracGraph_Europe.png, width(1275) replace
graph export FracGraph_Europe.pdf, replace



twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line ef year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfrac year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2003) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
*/


 // OUTPUT FIGURE A5
tsline totarea etharea netharea, yla(0(100000)500000) xla(1800(50)2003) ylabel(, angle(0)) ytitle(Mean state size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 graph export Trends_Europe.png, width(1275) replace
graph export Trends_Europe.pdf, replace

 
 
 




////////////////////////////////////////////////////////////////////////
// Create bar charts
////////////////////////////////////////////////////////////////////////
   
gen year5 = int((year+2)/5)
//replace year = year5

collapse (sum)bc secessiona collapsea transfera absorbb ethseca irra ethabsorbb ethcollapsea, by (year5)


gen secession = .
gen sum2 = .
gen sum3 = .
replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb

cd $OUTPUTDIR
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)400)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export BorderChange_Europe.png, width(1275) replace
graph export BorderChange_Europe.pdf, replace

 
 
replace secession = ethseca + ethcollapsea
replace sum2 = secession + irra
replace sum3 = sum2 + ethabsorbb

cd $OUTPUTDIR
twoway bar bc year, bcolor(ltblue) ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)400)  ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export EthBorderChange_Europe.png, width(1275) replace
graph export EthBorderChange_Europe.pdf, replace

  
  


/*

xxx


tsline ef agfrac, xla(1880(20)2020) yla(0(0.1)0.4) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Territorial Frac. kin AG")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efpw agfracpw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 



tsline efpw ef1 ef2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline efpw efpw1 efpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline agfracpw agfracpw1 agfracpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)


xxx
 tsline lntotarea lntotetharea lntotnetharea
 tsline totarea2 etharea2 netharea2, yla(0(1000000)3000000) xla(1880(20)2020)  ///
legend(order(1 "Actual area" 2 "Ethnic changes" 3 "Non-ethnic changes")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 
 xxx
 
 // Export data for combined area graph
 cd "/Users/larsc/polybox/Shared/NASTACdb/StateSizeProject/OutputCShapesagg"
 rename area cshp_area
 save cshp_system.dta, replace
 
 
 
 tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)

 
tsline dn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline ethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline nethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline totarea etharea netharea, yla(0(1000000)3000000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 

 
 
tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
graphregion(color(white)) bgcolor(white) scheme(s2mono)

tsline n, xla(1880(20)2020) yla(0(50)200) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)


twoway (line area year, yaxis(1)) (line efaw year, yaxis(2)) (line agfracaw year, yaxis(2)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)




// color
twoway (line area year, yaxis(1) lwidth(thick)) (line efaw year, yaxis(2) lcolor(red) lwidth(thick)) (line agfracaw year, yaxis(2) lcolor(green) lwidth(thick)), /// 
graphregion(color(white)) bgcolor(white) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size) 

// BW

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line ef year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfrac year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line intdjaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line extdjaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Internal Disjunction" 3 "External Disjunction")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshareaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshareaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Internal share" 3 "External share")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshare year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshare year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Dominant share" 3 "Spillover share")) ///
ytitle(Mean State Size)


gen secession = .
gen sum2 = .
gen sum3 = .

replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
 
 

replace sum2 = ethseca + irra
replace sum3 = sum2 + ethabsorbb
twoway bar bc year, bcolor(ltblue) ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020) ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
  

  
  */
